# -*- coding: utf-8 -*-
import re
import time
import json

from scrapy import Request
from scrapy_redis.spiders import RedisSpider
from urllib.parse import urlsplit, parse_qsl, unquote

from amac_spider.utils.handler import get_date, get_datetime, get_url
from amac_spider.items import PofPersonOrganizationListItem, PofPersonPersonListItem, PofPersonPersonCertStatusChangeRecordItem


class PofPersonSpider(RedisSpider):
    name = 'pof_person'

    def parse(self, response):
        org_item = PofPersonOrganizationListItem()
        info_list = json.loads(response.text)['content']
        mappings = {'org_id': 'userId', 'org_name': 'orgName', 'org_type': 'orgType',
                    'worker_total': 'extWorkerTotalNum', 'oper_num': 'operNum', 'sales_num': 'salesmanNum',
                    'fund_manager_num': 'fundManagerNum', 'investment_manager_num': 'investmentManagerNum'}
        query = dict(parse_qsl(urlsplit(response.url).query))['query']
        for info in info_list:
            for k, v in mappings.items():
                org_item[k] = info[v]
            person_list_url = get_url(self.name) + '/personList.html?userId={}'.format(org_item['org_id'])
            org_item['person_list_url'] = person_list_url
            org_item['search_field'] = query
            org_item['crawl_time'] = get_datetime(time.time())
            org_item['type'] = 'list'

            headers = {'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest'}
            args = unquote(response.url).rsplit('?')[1]
            person_list_api_url = re.sub(r'res', 'api', get_url(self.name), 1) + '?{}'.format(args)
            body = {'page': 1, 'userId': org_item['org_id']}
            yield org_item
            yield Request(url=person_list_api_url, method='POST', headers=headers, body=json.dumps(body),
                          callback=self.parse_person_list_and_detail, dont_filter=True, meta={'query': query,
                                                                                              'person_list_url': person_list_url,
                                                                                              'org_id': info['userId']})

    def parse_person_list_and_detail(self, response):
        person_item = PofPersonPersonListItem()
        info_list = json.loads(response.text)['content']
        mappings = {'person_id': 'accountId', 'name': 'userName', 'sex': 'sex', 'org_name': 'orgName', 'cert_code': 'certCode',
                    'cert_name': 'certName', 'cert_status': 'statusName', 'cert_obtain_time': 'certObtainDate',
                    'cert_status_change_times': 'certStatusChangeTimes', 'credit_record_num': 'creditRecordNum'}
        change_record_mappings = {'cert_code': 'certCode', 'cert_change_time': 'creationDate', 'org_name': 'orgName',
                                  'cert_name': 'certName', 'cert_status': 'statusName'}
        cert_status_change_record = PofPersonPersonCertStatusChangeRecordItem()
        for info in info_list:
            for k, v in mappings.items():
                person_item[k] = info[v]
            for record in info['personCertHistoryList']:
                for key, value in change_record_mappings.items():
                    if key == 'cert_change_time':
                        cert_status_change_record[key] = get_date(record[value]/1000)
                    else:
                        cert_status_change_record[key] = record[value]
                cert_status_change_record['type'] = 'cert_status_change_record'
                cert_status_change_record['person_id'] = person_item['person_id']
                cert_status_change_record['name'] = person_item['name']
                cert_status_change_record['org_id'] = response.meta.get('org_id')
                cert_status_change_record['person_detail_url'] = get_url(self.name) + '/personDetail.html?accountId={}'.format(person_item['person_id'])
                cert_status_change_record['crawl_time'] = get_datetime(time.time())
                yield cert_status_change_record
            person_item['org_id'] = response.meta.get('org_id')
            person_item['person_list_url'] = response.meta.get('person_list_url')
            person_item['cert_obtain_time'] = get_date(person_item['cert_obtain_time']/1000)
            person_item['person_detail_url'] = get_url(self.name) + '/personDetail.html?accountId={}'.format(person_item['person_id'])
            person_item['search_query'] = response.meta['query']
            person_item['crawl_time'] = get_datetime(time.time())
            person_item['type'] = 'detail'
            yield person_item

    def make_requests_from_url(self, url):
        headers = {
            'Content-Type': 'application/json',
            'X-Requested-With': 'XMLHttpRequest'
        }
        url = url + '&type=list'
        query = dict(parse_qsl(urlsplit(url).query))['query']
        data = {
            'orgType': query,
            'page': 1
        }
        return Request(url=url, method='POST', headers=headers, body=json.dumps(data), dont_filter=True)
